/**
 * Copyright 2009 Sergio Bossa
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.googlecode.actorom.impl.remote;

import com.googlecode.actorom.Actor;
import com.googlecode.actorom.impl.core.AbstractTopology;
import com.googlecode.actorom.impl.core.ThreadingPolicy;
import com.googlecode.actorom.support.ThreadingPolicies;
import com.googlecode.actorom.impl.remote.channel.MessageChannel;
import com.googlecode.actorom.impl.remote.channel.TopologyCallback;
import com.googlecode.actorom.impl.remote.support.RemoteActorProxy;
import com.googlecode.actorom.support.TopologyException;
import java.io.IOException;
import net.jcip.annotations.ThreadSafe;

/**
 * {@link com.googlecode.actorom.Topology} implementation based on sockets and asynchronous I/O,
 * enabling communication between different, remote, topologies and actors.<br>
 * Every RemoteTopology instance is bound to a specific host address and port, and is able to:
 * <ul>
 * <li>Locally spawn actors.</li>
 * <li>Access its own locally spawned actors, as well as other RemoteTopology actors by using their {@link RemoteAddress}.</li>
 * </ul>
 * So, actors belonging to different RemoteTopology instances are able to communicate and exchange messages.
 *
 * @author Sergio Bossa
 */
@ThreadSafe
public class RemoteTopology extends AbstractTopology<RemoteAddress> {

    private final String host;
    private final int port;
    private final MessageChannel messageChannel;

    public RemoteTopology(String host, int port) {
        this(host, port, ThreadingPolicies.newGreenThreadingPolicy());
    }

    public RemoteTopology(String host, int port, ThreadingPolicy threadingPolicy) {
        super(threadingPolicy);
        try {
            this.host = host;
            this.port = port;
            messageChannel = new MessageChannel(host, port, new TopologyCallbackImpl());
            messageChannel.bind();
        } catch (IOException ex) {
            throw new TopologyException(ex.getMessage(), ex);
        }
    }

    public RemoteAddress spawnActor(String id, Object handler) {
        RemoteAddress address = new RemoteAddress(host, port, id);
        boolean spawned = doSpawnActor(address, handler);
        if (spawned) {
            return address;
        } else {
            return null;
        }
    }

    public Actor getActor(RemoteAddress address) {
        if (address == null) {
            return null;
        } else {
            if (!shutdown) {
                Actor localActor = doGetActor(address);
                if (localActor != null) {
                    return localActor;
                } else {
                    return new RemoteActorProxy(address);
                }
            } else {
                return null;
            }
        }
    }

    public void shutdown() {
        messageChannel.unbind();
        doShutdown();
    }

    private class TopologyCallbackImpl implements TopologyCallback {

        public boolean sendMessage(RemoteAddress recipientAddress, Object message) {
            Actor recipientActor = doGetActor(recipientAddress);
            if (recipientActor != null) {
                recipientActor.send(message);
                return true;
            } else {
                return false;
            }
        }
    }
}
